package com.example.common;


import com.sun.jna.Library;
import com.sun.jna.Native;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @class:PrintBarcodeUtil
 * @descript:java使用编程接口打印条形码
 * @date:2016年12月8日 下午2:02:50
 * @version:V1.0
 */
class PrintBarcodeUtil {
    //加载库文件
    private static final String LOAD_LIBRARY = "TSCLIB";
    //打印机型号
    private static final String print_model = "Gprinter  GP-1124D";

    /**
     * @class:TscLibDll
     * @descript:创建TSCLIB.dll编程接口 动态链接库TSCLIB.dll，支持大部分佳博打印机，和其他品牌的TSC系列的打印机
     * @date:2016年12月8日 下午2:03:55
     * @version:V1.0
     */
    public interface TscLibDll extends Library {
        TscLibDll INSTANCE = (TscLibDll) Native.loadLibrary("TSCLIB", TscLibDll.class);

        int about();

        int openport(String pirnterName);

        int closeport();

        int sendcommand(String printerCommand);

        int setup(String width, String height, String speed, String density, String sensor, String vertical, String offset);

        int downloadpcx(String filename, String image_name);

        int barcode(String x, String y, String type, String height, String readable, String rotation, String narrow, String wide, String code);

        int printerfont(String x, String y, String fonttype, String rotation, String xmul, String ymul, String text);

        int clearbuffer();

        int printlabel(String set, String copy);

        int formfeed();

        int nobackfeed();

        int windowsfont(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, String content);
    }

    public static void printBarcode(String message, String text) {
        //加载驱动
        System.loadLibrary(LOAD_LIBRARY);
        //解决中文乱码
        System.setProperty("jna.encoding", "GBK");
        //TscLibDll.INSTANCE.about();
        TscLibDll.INSTANCE.openport(print_model);
        //TscLibDll.INSTANCE.downloadpcx("C:\\UL.PCX", "UL.PCX");
        //TscLibDll.INSTANCE.sendcommand("REM ***** This is a test by JAVA. *****");
        TscLibDll.INSTANCE.setup("100", "60", "5", "8", "0", "0", "0");
        TscLibDll.INSTANCE.clearbuffer();
        TscLibDll.INSTANCE.printerfont("150", "270", "TSS24.BF2", "0", "1", "1", text);
        TscLibDll.INSTANCE.barcode("150", "90", "128", "150", "1", "0", "2", "2", message);
        //TscLibDll.INSTANCE.windowsfont(400, 200, 48, 0, 3, 1, "arial", "DEG 0");
        TscLibDll.INSTANCE.printlabel("1", "1");
        TscLibDll.INSTANCE.closeport();
    }

    //测试
    public static void main(String[] args) throws UnsupportedEncodingException {
        List<String> list = new ArrayList<String>();
        list.add("KJ4.1-0127-0001(001)");
        list.add("KJ4.1-0128-0001(001)");
        list.add("KJ4.1-0129-0001(001)");
        list.add("KJ4.1-0130-0001(001)");
        if (list != null && list.size() > 0) {
            for (String message : list) {
                printBarcode(message, "苏交科");
            }
        }
        System.out.println("打印成功");
    }
}
